var did_1 = 'XIoin4sGcZmeYvwEMFRNtp';
var did_2 = 'hgvjyLMXlYejcmfF8Ked8v';

function login() {
	msg.loading('登录中');
	mui.ajax('https://api.gizwits.com/app/users', {
		data: {
			phone_id: '13809734334'
		},
		dataType: 'json', 
		type: 'post', 
		timeout: 10000,
		headers: {
			'Content-Type': 'application/json',
			'X-Gizwits-Application-Id': '7504c477fa0141d6a9f1df82f9202c22'
		},
		success: function(data) {
			msg.close();
			console.log(data);
			localStorage.setItem("token", data['token']);
			
			setTimeout(devdata1,500);
			setTimeout(devdata2,1000);
		},
		error: function(xhr, type, errorThrown) {
			msg.close();
			console.log(type);
			
			login();
		}
	});
}

function bindings() {
	msg.loading('绑定设备');
	mui.ajax('http://api.gizwits.com/app/bindings', {
		data: {
			"devices": [{
				"did": "YH4nEvArYZhBEmwrnf2kq3",
				"passcode": "123456",
				"remark": "",
				"dev_alias": ""
			}]
		},
		dataType: 'json', 
		type: 'post', 
		timeout: 10000, 
		headers: {
			'Content-Type': 'application/json',
			'X-Gizwits-Application-Id': 'bdb0d820a5e843f8aa61b8ba72af8d68',
			'X-Gizwits-User-token': localStorage.getItem("token")
		},
		success: function(data) {
			msg.close();
			console.log(data);
		},
		error: function(xhr, type, errorThrown) {
			msg.close();
			console.log(type);
		}
	});
}

function control() {
	msg.loading('加载中');
	mui.ajax('http://api.gizwits.com/app/control/YH4nEvArYZhBEmwrnf2kq3', {
		data: {
			"attrs": {
				"bool_valueError": true
			}
		},
		dataType: 'json', 
		type: 'post', 
		timeout: 10000, 
		headers: {
			'Content-Type': 'application/json',
			'X-Gizwits-Application-Id': 'bdb0d820a5e843f8aa61b8ba72af8d68',
			'X-Gizwits-User-token': localStorage.getItem("token")
		},
		success: function(data) {
			msg.close();
			console.log(data);
		},
		error: function(xhr, type, errorThrown) {
			msg.close();
			console.log(type);
		}
	});
}

function devdata1() {
	mui.ajax('https://api.gizwits.com/app/devices/'+did_1+'/raw_data', {
		data: {
			type: 'cmd',
			start_time: Math.floor(new Date(new Date().getTime()-24*60*60*1000).getTime()/1000),
			end_time: Math.floor(new Date().getTime()/1000),
			limit: '100'
		},
		dataType: 'json', 
		type: 'get', 
		timeout: 10000, 
		headers: {
			'X-Gizwits-Application-Id': '7504c477fa0141d6a9f1df82f9202c22',
			'X-Gizwits-User-token': localStorage.getItem("token")
		},
		success: function(data) {
			console.log(data);
			var json = JSON.parse(data);
			console.log(json['objects']);

			var arr = json['objects'];
			if(arr.length != 0) {
				document.getElementById("ul_1").innerHTML = '';
			}
			
			var kv = new Map();
			kv.set('0004b383a23f7480', '伍光豪');
			kv.set('00e92a92a23f7480', '陈文彪');
			kv.set('00a2f740a23f7480', '姜忠伟');
			kv.set('00f1d495a23f7480', '张能滔');
			kv.set('0007c077a23f7480', '毛德军');
			
			for (i = 0; i < arr.length; i++) {
				console.log(arr[i]['payload_bin'].slice(-16));
				
				var uid = arr[i]['payload_bin'].slice(-16);
				var timestamp = arr[i]['timestamp'];

				var li = document.createElement("li");
				li.setAttribute("class", "mui-table-view-cell");
				
				var div = document.createElement("div");
				div.setAttribute("class", "row");
				li.appendChild(div);
				
				var div1 = document.createElement("div");
				div1.setAttribute("class", "col-xs-3");
				div1.innerHTML = i+1;
				div.appendChild(div1);
				
				var div2 = document.createElement("div");
				div2.setAttribute("class", "col-xs-3");
				div2.innerHTML = kv.get(uid) != null ? kv.get(uid): '未知';
				div.appendChild(div2);
				
				var div3 = document.createElement("div");
				div3.setAttribute("class", "col-xs-6");
				div3.innerHTML = toTime(timestamp);
				div.appendChild(div3);
				
				document.getElementById("ul_1").appendChild(li);
			}
			
			setTimeout(devdata1,100);
		},
		error: function(xhr, type, errorThrown) {
			console.log(type);
			setTimeout(devdata1,100);
		}
	});
}

function devdata2() {
	console.log('devdata2');
}

function toTime(t) {
	var date = new Date(t * 1000);  
	Y = date.getFullYear() + '-';
	M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
	D = date.getDate() + ' ';
	h = date.getHours() + ':';
	m = date.getMinutes() + ':';
	s = date.getSeconds();
	
	return Y+M+D+h+m+s;
}